import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../services/searchServices.dart';

class SearchController extends GetxController {
  // 搜索内容
  String keyWords = "";
  RxList historyList = [].obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getHistoryData();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  // 获取本地历史记录
  getHistoryData() async {
    var tempList = await SearchServices.getHistoryData();
    if (tempList.isNotEmpty) {
      historyList.addAll(tempList);
      update();
    }
  }

  // 清空数据
  clearHistoryData() async {
    await SearchServices.clearHistoryData();
    historyList.clear();
    update();
  }

  // 删除数据
  deleteHistoryData(keyWords) async {
    SearchServices.deleteHistoryData(keyWords);
    historyList.remove(keyWords);
    update();
  }
}
